package fr.resasante.spring3.controller.view;

import java.util.HashMap;
import java.util.Map;

import javax.naming.NamingException;

import org.springframework.web.servlet.ModelAndView;

import fr.resasante.ejb.dto.User;
import fr.resasante.ejb.utils.EjbResponse;
import fr.resasante.spring3.form.UserForm;
import fr.resasante.utils.AgreementsUtils;
import fr.resasante.utils.TitlesUtils;

/**
 * Class for getting system properties
 * 
 * @author GEFFROY-DE-VILLEBLAN
 * 
 */
public final class RegisterView {
	/**
	 * class instance
	 */
	private static volatile RegisterView instance = null;

	/**
	 * Method returning a Singleton class instance
	 * 
	 * @return Singleton instance
	 * @throws NamingException
	 */
	public final static RegisterView getInstance() {
		if (RegisterView.instance == null) {
			synchronized (RegisterView.class) {
				if (RegisterView.instance == null) {
					RegisterView.instance = new RegisterView();
				}
			}
		}
		return RegisterView.instance;
	}

	/**
	 * Constructor
	 */
	private RegisterView() {
		super();
	}

	/**
	 * Return the objects and view for account retrievable
	 * 
	 * @param user
	 *            The user form
	 * @return The view
	 */
	public ModelAndView getRegisterLostView(User user) {
		return getRegisterLostView(user, false);
	}

	/**
	 * Return the objects and view for account retrieval
	 * 
	 * @param user
	 *            The user form
	 * @param errorCaptcha
	 *            If the captcha is wrong
	 * @return The view
	 */
	public ModelAndView getRegisterLostView(User user, boolean errorCaptcha) {
		final ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("register/lost");
		modelAndView.addObject("lost", user);
		if (errorCaptcha) {
			modelAndView.addObject("captchaError", 1);
		}
		return modelAndView;
	}

	/**
	 * Return the objects and view for account retrieval with errors
	 * 
	 * @param userForm
	 *            The patient form object
	 * @param ejbResponse
	 *            The EJB response containing errors
	 * @return The view
	 */
	public ModelAndView getRegisterLostViewWithError(User user, EjbResponse ejbResponse) {
		final ModelAndView modelAndView = getRegisterLostView(user, false);
		modelAndView.addObject("errorList", ejbResponse.getErrors());
		return modelAndView;
	}

	/**
	 * Return the objects and view for register form
	 * 
	 * @param userForm
	 *            The register form bean
	 * @return The view
	 */
	public ModelAndView getRegisterPatientView(UserForm userForm) {
		return getRegisterPatientView(userForm, false);
	}

	/**
	 * Return the objects and view for register form
	 * 
	 * @param userForm
	 *            The register form bean
	 * @param captchaError
	 *            If there is a captcha error to display
	 * @return The view
	 */
	public ModelAndView getRegisterPatientView(UserForm userForm, boolean captchaError) {
		final ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("register/patient");
		modelAndView.addObject("register", userForm);
		if (captchaError) {
			modelAndView.addObject("captchaError", 1);
		}
		final Map<String, String> genderList = new HashMap<String, String>();
		genderList.put("label.mister", "Homme");
		genderList.put("label.mises", "Femme");
		modelAndView.addObject("genderList", genderList);
		return modelAndView;
	}

	/**
	 * Return the objects and view for register form with errors
	 * 
	 * @param userForm
	 *            The patient form object
	 * @param ejbResponse
	 *            The EJB response containing errors
	 * @return The view
	 */
	public ModelAndView getRegisterPatientViewWithError(UserForm userForm,
			EjbResponse ejbResponse) {
		final ModelAndView modelAndView = getRegisterPatientView(userForm, false);
		modelAndView.addObject("errorList", ejbResponse.getErrors());
		return modelAndView;
	}

	/**
	 * Return the objects and view for register form
	 * 
	 * @param userForm
	 *            The register form bean
	 * @return The view
	 */
	public ModelAndView getRegisterProView(UserForm userForm) {
		return getRegisterProView(userForm, false);
	}

	/**
	 * Return the objects and view for register form
	 * 
	 * @param userForm
	 *            The register form bean
	 * @param captchaError
	 *            If there is a captcha error to display
	 * @return The view
	 */
	public ModelAndView getRegisterProView(UserForm userForm, boolean captchaError) {
		final ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("register/professional");
		modelAndView.addObject("register", userForm);
		if (captchaError) {
			modelAndView.addObject("captchaError", 1);
		}
		modelAndView.addObject("titleList", TitlesUtils.getInstance().getAllTitles());
		modelAndView.addObject("agreementList", AgreementsUtils.getInstance()
				.getAllAgreements());
		final Map<String, String> genderList = new HashMap<String, String>();
		genderList.put("label.mister", "Homme");
		genderList.put("label.mises", "Femme");
		modelAndView.addObject("genderList", genderList);
		return modelAndView;
	}

	/**
	 * Return the objects and view for register form with errors
	 * 
	 * @param userForm
	 *            The professional form object
	 * @param ejbResponse
	 *            The EJB response containing errors
	 * @return The view
	 */
	public ModelAndView getRegisterProViewWithError(UserForm userForm, EjbResponse ejbResponse) {
		final ModelAndView modelAndView = getRegisterProView(userForm, false);
		modelAndView.addObject("errorList", ejbResponse.getErrors());
		return modelAndView;
	}

	/**
	 * Return the objects and view for success
	 * 
	 * @param resultMessageKey
	 *            The key of the message to print
	 * @return The view
	 */
	public ModelAndView getSuccessView(String resultMessageKey) {
		// If everything goes fine
		final ModelAndView modelAndView = new ModelAndView("success");
		modelAndView.addObject("resultMessage", resultMessageKey);
		return modelAndView;
	}

}